//
//  UUDriverEditVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUDriverEditVC.h"
#import "UUHybridManager+CharterSupplier.h"
#import "UUDriverManager.h"
#import "UUFleetManager.h"
#import "UUPermissionManager.h"
#import <UUFoundation/DKProgressHUD.h>

@interface UUDriverEditVC () <UUDriverManagerProtocol>

@end

@implementation UUDriverEditVC

- (void)viewDidLoad {
    if (!self.editable ) {
        [GetDriverManager() addObserver:self];
    }
    
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)name {
    return @"司机";
}

- (BOOL)isAdd {
    return self.driverId == nil;
}

- (BOOL)canEdit {
    return [GetPermissionManager() checkPermission:UUPermissionFlagDriverReadWrite];
}

- (NSURLRequest *)editRequest {
    return GetHybridManager().driverEditURLRequest;
}

- (NSURLRequest *)detailRequest {
    return GetHybridManager().driverDetailURLRequest;
}

- (void)configEditVC:(UUDriverEditVC *)editVC {
    editVC.driverId = self.driverId;
}

- (void)addOrUpdate:(NSDictionary *)data {
    NSNumber *fleetId = data[@"motorcade_id"];
    NSString *name = data[@"name"];
    NSString *mobile = data[@"mobile"];
    
    WEAK_SELF();
    void (^completeBlock)(NSError *error) = ^(NSError *error) {
        HideIndicatorInView(weakSelf.view);
        
        if (error == nil) {
            NSString *tips = [NSString stringWithFormat:@"%@成功", weakSelf.driverId == nil ? @"添加" : @"更新"];
            ShowTipsInKeyWindow(tips);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            ShowTipsOnView(error.localizedDescription, weakSelf.view);
        }
    };
    
    ShowIndicator();
    
    if (self.driverId == nil) {
        [GetDriverManager() addDriverWithFleetId:fleetId
                                            name:name
                                          mobile:mobile
                                   completeBlock:completeBlock];
    } else {
        [GetDriverManager() updateDriverWithDriverId:self.driverId
                                             fleetId:fleetId
                                                name:name
                                              mobile:mobile
                                       completeBlock:completeBlock];
    }
}

- (NSDictionary *)previewFields {
    if (self.driverId != nil) {
        UUDriverModel *driver = [GetDriverManager() fetchDriverWithDriverId:self.driverId];
        UUFleetModel *fleet = [GetFleetManager() fetchFleetWithFleetId:driver.fleetId];
        return @{
                 @"id" : self.driverId,
                 @"motorcade_id" : driver.fleetId,
                 @"motorcade_name" : fleet.name,
                 @"name" : driver.name,
                 @"mobile" : driver.mobile,
                 };
    } else {
        return nil;
    }
}

#pragma mark - UUDriverManagerProtocol methods

- (void)driverManagerDidUpdateDrivers {
    if (self.driverId != nil) {
        [self reloadForm];
    }
}

@end
